#pragma once

#include  "iostream"
#include  "vector"
#include  "stack"
#include  "unordered_map"
#include   "queue"
#include  "TypeDefin.h"

using namespace std;

// 716 592
// 219

ListNode *reverse(ListNode *root) {
    if (root == nullptr || root->next == nullptr)
        return root;

    auto ret = reverse(root->next);

    root->next->next = root;
    root->next = nullptr;

    return ret;

}

ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {

    ListNode *res = nullptr;
    ListNode *ret = nullptr;
    int left = 0;
    int val = 0;
    while (l1 && l2) {
        int sum = l1->val + l2->val + left;
        left = sum / 10;
        if (!ret) {
            res = new ListNode(sum % 10);
            ret = res;
        } else {
            res->next = new ListNode(sum % 10);
            res = res->next;
        }

    }
    while (l1) {
        int sum = l1->val + left;
        left = sum / 10;
        res->next = new ListNode(sum % 10);
        res = res->next;
    }
    while (l2) {
        int sum = l2->val + left;
        left = sum / 10;
        res->next = new ListNode(sum % 10);
        res = res->next;
    }

    if (left != 0) {
        res->next = new ListNode(left);
    }

    return ret;//reverse(ret);
}